<?php

/**
 * @author dungdhs@gmail.com
 * @copyright 2012
 */

function user_task(){
  return array(
    'login' => array(
      'func'  => '_user_login',
      'perm'  => 'user login',
			'title'	=> 'User login' 
    ),
    'register' => array(
      'func'  => '_user_register',
      'perm'  => 'user register',
			'title'	=> 'User register' 
    ),
    'logout' => array(
      'func'  => '_user_logout',
      'perm'  => 'user logout'
    ),
    'edit' => array(
      'func'  => '_user_edit',
      'perm'  => 'user edit'
    )
  );
}

function user_init(){
  global $smarty, $database;
  
  theme_js('dz.user.data='.json_encode(user_data()));    
  theme_js('dz.user.address='.json_encode(getCityData()));
}

function user_process_page(){
  global $smarty;
  $smarty->assign('user', user_data());
}

function user_data(){
  global $user, $session;
  return array(
    'user_id'         => isset($user->user_info['user_id'])?$user->user_info['user_id']:0,
    'user_email'      => isset($user->user_info['user_email'])?$user->user_info['user_email']:"",
    'user_username'   => isset($user->user_info['user_username'])?$user->user_info['user_username']:"",
    'user_fullname'   => isset($user->user_info['user_fullname'])?$user->user_info['user_fullname']:"",
    'user_address'    => isset($user->user_info['user_address'])?$user->user_info['user_address']:"",
    'user_district'   => isset($user->user_info['user_district'])?$user->user_info['user_district']:0,
    'user_city'       => isset($user->user_info['user_city'])?$user->user_info['user_city']:0,
    'user_mobile'     => isset($user->user_info['user_mobile'])?$user->user_info['user_mobile']:"",
    'user_verified'   => isset($user->user_info['user_verified'])?$user->user_info['user_verified']:0,
    'user_gold'   => isset($user->user_info['user_gold'])?$user->user_info['user_gold']:0,
    'user_logout_url' => l('user/logout/'.$session->id())
  );
}

function _user_login(){
	global $user, $database, $smarty;
	
	if ($user->user_exists) cheader('homepage');
	
	if (isset($_POST['user_email'])){
		$user_email			= PGRequest::getVar('user_email');
		$user_password	= PGRequest::getVar('user_password');
		
		$user->user_login( array($user_email), $user_password, 0, 1);
		
		if ($user->is_error){
			
		}else{
			cheader('homepage');
		}
	}
	
	return $smarty->fetch('user_login.tpl');
}

function _user_edit(){
	global $database, $smarty, $user;
	
	$user_id = arg(2);
	
	$u = new PGUser(array($user_id));
	if (!$u->user_exists) cheader('homepage');
	
	$smarty->assign('u', $u->user_info);
	
	theme_title('User information');
	
	return $smarty->fetch('user_edit.tpl');
}
?>
